<template>
  <div class="account-box">

    <!-- A1 - 点击切换按钮,选择手机号登录还是账号密码登录 -->
    <div class="toggle">
      <a @click="changeLogin(false)" href="javascript:;" v-if="isMsgLogin">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a @click="changeLogin(true)" href="javascript:;" v-else>
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>

    <!-- A2 - 表单区域-使用作用域插槽 -->
    <Form ref='loginForm' class="form" v-slot='{ errors }'>

      <!--A2 -1 账号登录 -->
      <template v-if="!isMsgLogin">
        <!-- 账号 -->
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field v-model='form.account' :rules='schema.account' name='account' autocomplete='off' type="text" placeholder="请输入用户名或手机号" />
          </div>
          <!-- 表单验证错误提示 -->
          <div class="error" v-if='errors.account'><i class="iconfont icon-warning" />{{errors.account}}</div>
        </div>

        <!-- 密码 -->
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <Field v-model='form.password' :rules='schema.password' name='password' type="password" placeholder="请输入密码" />
          </div>
          <div class="error" v-if='errors.password'><i class="iconfont icon-warning" />{{errors.password}}</div>
        </div>
      </template>

      <!--A2 -2 短信登录 -->
      <template v-else>
        <!-- 手机号 -->
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field autocomplete="off" v-model='form.mobile' :rules='schema.mobile' name='mobile' type="text" placeholder="请输入手机号" />
          </div>
          <div class="error" v-if='errors.mobile'><i class="iconfont icon-warning" />{{errors.mobile}}</div>
        </div>
        <!-- 验证码 -->
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <Field autocomplete="off" v-model='form.code' :rules='schema.code' name='code' type="password" placeholder="请输入验证码" />
            <span class="code">发送验证码</span>
          </div>
          <div class="error" v-if='errors.code'><i class="iconfont icon-warning" />{{errors.code}}</div>
        </div>
      </template>

      <!--A2 -3 隐私条款 -->
      <div class="form-item">
        <div class="agree">
          <!-- <XtxCheckbox v-model="form.isAgree" /> -->
          <Field as='XtxCheckbox' :rules='schema.isAgree' v-model="form.isAgree" name='isAgree' />
          <span>我已同意</span>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <div class="error" v-if='errors.isAgree'><i class="iconfont icon-warning" />{{errors.isAgree}}</div>
      </div>

      <!--A2 -4 登录按钮 -->
      <a href="javascript:;" @click="handleLogin" class="btn">登录</a>
    </Form>

    <!-- A3 - 免费注册,忘记密码 -->
    <div class="action">
      <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="">
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>

  </div>
</template>
<script>
// P1-引入ref, reactive
import { ref, reactive } from 'vue'

// P2-从第三方的包中引入Form, Field组件
import { Form, Field } from 'vee-validate'

// P3-定义一个规则文件,专门用作规则验证
import schema from '@/utils/vee-validate-schema.js'

// P4-引入消息提示弹框的方法,该方法代表的是消息提示组件
import Message from '@/components/library/Message'
export default {
  name: 'LoginForm',
  components: { Form, Field },
  setup () {
    // A1 -通过ref绑定,form表单DOM
    const loginForm = ref(null)

    // A2 -默认表单数据
    const form = reactive({
      // 用户名
      account: null,
      // 密码
      password: null,
      // 手机号
      mobile: null,
      // 验证码
      code: null,
      // 是否同意
      isAgree: false
    })

    // A3 -登录方式标志位
    const isMsgLogin = ref(false)

    // A4 -切换登录方式
    const changeLogin = (type) => {
      // A4 -1 登录方式改变成选中的值
      isMsgLogin.value = type
      // A4 -2 切换登录方式时，需要清空表单
      form.account = null
      form.password = null
      form.mobile = null
      form.code = null
      form.isAgree = false
      // A4 -3 直接操作组件实例的方法resetForm，用于重置表单（可以重置错误提示）
      loginForm.value.resetForm()
    }

    // A5 -点击登录按钮
    const handleLogin = async () => {
      // 手动进行表单验证
      const flag = await loginForm.value.validate()
      if (flag) {
        // 验证通过
        Message({ type: 'success', text: '成功' })
      }
    }

    return { isMsgLogin, form, schema, changeLogin, loginForm, handleLogin }
  }
}
</script>
<style scoped lang='less'>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 40px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
